package com.pet.platform.controller;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.AppointmentDTO;
import com.pet.platform.dto.AppointmentQueryDTO;
import com.pet.platform.entity.Appointment;
import com.pet.platform.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/appointments")
public class AppointmentController {
    
    @Autowired
    private AppointmentService appointmentService;
    
    @GetMapping("/{id}")
    public Result<Appointment> findById(@PathVariable Long id) {
        return Result.success(appointmentService.findById(id));
    }
    
    @GetMapping
    public Result<PageResult<AppointmentDTO>> findPage(AppointmentQueryDTO queryDTO) {
        return Result.success(appointmentService.findPage(queryDTO));
    }
    
    @PostMapping
    public Result<Void> create(@Valid @RequestBody AppointmentDTO appointmentDTO) {
        appointmentService.create(appointmentDTO);
        return Result.success();
    }
    
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @Valid @RequestBody AppointmentDTO appointmentDTO) {
        appointmentService.update(id, appointmentDTO);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        appointmentService.delete(id);
        return Result.success();
    }
    
    @PutMapping("/{id}/status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> updateStatus(@PathVariable Long id, @RequestParam String status) {
        appointmentService.updateStatus(id, status);
        return Result.success();
    }
    
    @GetMapping("/check-conflict")
    public Result<Boolean> checkTimeConflict(@RequestParam Long serviceId, @Valid AppointmentDTO appointmentDTO) {
        return Result.success(appointmentService.checkTimeConflict(serviceId, appointmentDTO));
    }
} 